<?php
//load CSVReader class
Loader::loadClass('CSVReader', "modules/iCarrent/pnincludes");
 /**
  * initialise module
  *
  */
function iCarrent_init() {
  //iCarrent_delete();
  if (!DBUtil::createTable('icarrent_setting')) {
      return false;
  }
  if (!DBUtil::createTable('icarrent_type')) {
      return false;
  }
  if (!DBUtil::createTable('icarrent_category')) {
      return false;
  }
  if (!DBUtil::createTable('icarrent_subcategory')) {
      return false;
  }

  //Add new 
  if (!DBUtil::createTable('icarrent_brand')) {
      return false;
  }
  if (!DBUtil::createTable('icarrent_model')) {
      return false;
  }
  if (!DBUtil::createTable('icarrent_carproperty')) {
      return false;
  }
  if (!DBUtil::createTable('icarrent_subcategory_brand')) {
      return false;
  }
  if (!DBUtil::createTable('icarrent_subcategory_carproperty')) {
      return false;
  }
  if (!DBUtil::createTable('icarrent_subcategory_model')) {
      return false;
  }
  if (!DBUtil::createTable('icarrent_topics')) {
      return false;
  }
  if (!DBUtil::createTable('icarrent_posts')) {
      return false;
  }
  if (!DBUtil::createTable('icarrent_resource_image')) {
      return false;
  }
  if (!DBUtil::createTable('icarrent_favourite')) {
      return false;
  }
  if (!DBUtil::createTable('icarrent_teamtopic')) {
      return false;
  }
  if (!DBUtil::createTable('icarrent_amount_topic')) {
      return false;
  }
  if (!DBUtil::createTable('icarrent_shop')) {
      return false;
  }
  if (!DBUtil::createTable('icarrent_province')) {
      return false;
  }
  
  if (!DBUtil::createTable('icarrent_province')) {
      return false;
  }
  
  if (!DBUtil::createTable('icarrent_province_airport')) {
      return false;
  }
  
  if (!DBUtil::createTable('icarrent_province_destination')) {
      return false;
  }  
    
    
  if (!DBUtil::createTable('icarrent_district')) {
      return false;
  }
  if (!DBUtil::createTable('icarrent_pickup_location')) {
      return false;
  }
  if (!DBUtil::createTable('icarrent_service_area')) {
      return false;
  }
  if (!DBUtil::createTable('icarrent_carlocation')) {
      return false;
  }
  if (!DBUtil::createTable('icarrent_reserve')) {
      return false;
  }


   init_setting();
   init_type();
   init_category();
   init_subcategory();
   init_brand();
   init_mobel();
   init_subcategory_brand();
   init_subcategory_carproperty();
   init_subcategory_model();

   init_province();
   init_district();
   init_property();


  createFilesDirectory();
  return true;
}

function iCarrent_delete() {
  // drop table
  DBUtil::dropTable('icarrent_setting');
  DBUtil::dropTable('icarrent_type');
  DBUtil::dropTable('icarrent_category');
  DBUtil::dropTable('icarrent_subcategory');

  DBUtil::dropTable('icarrent_brand');
  DBUtil::dropTable('icarrent_model');
  DBUtil::dropTable('icarrent_carproperty');
  DBUtil::dropTable('icarrent_subcategory_brand');
  DBUtil::dropTable('icarrent_subcategory_model');
  DBUtil::dropTable('icarrent_subcategory_carproperty');

  DBUtil::dropTable('icarrent_topics');
  DBUtil::dropTable('icarrent_teamtopic');
  DBUtil::dropTable('icarrent_posts');
  DBUtil::dropTable('icarrent_resource_image');
  DBUtil::dropTable('icarrent_favourite');
  DBUtil::dropTable('icarrent_shop');
  DBUtil::dropTable('icarrent_amount_topic');
  
  
  DBUtil::dropTable('icarrent_province');
  DBUtil::dropTable('icarrent_province_airport');
  DBUtil::dropTable('icarrent_province_destination');    

  
  DBUtil::dropTable('icarrent_district');
  DBUtil::dropTable('icarrent_pickup_location');
  DBUtil::dropTable('icarrent_service_area');
  DBUtil::dropTable('icarrent_carlocation');
  DBUtil::dropTable('icarrent_reserve');


  rmdir_recurse(DELETE_FILES_MAINPATH);
  return true;
}

function init_setting(){
  $dataArray = CSVReader::readcsv('modules/iCarrent/data/setting.csv');
  if ($dataArray){
    DBUtil::insertObjectArray($dataArray, 'icarrent_setting', true);
    unset($dataArray);
  }
}
function init_type(){
  $dataArray = CSVReader::readcsv('modules/iCarrent/data/type.csv');
  if ($dataArray){
    DBUtil::insertObjectArray($dataArray, 'icarrent_type', true);
    unset($dataArray);
  }
}
function init_category(){
  $dataArray = CSVReader::readcsv('modules/iCarrent/data/car_category.csv');
  if ($dataArray){
    DBUtil::insertObjectArray($dataArray, 'icarrent_category', true);
    unset($dataArray);
  }
}
function init_subcategory(){
  $dataArray = CSVReader::readcsv('modules/iCarrent/data/car_subcategory.csv');
  if ($dataArray){
    DBUtil::insertObjectArray($dataArray, 'icarrent_subcategory', true);
    unset($dataArray);
  }
}
function init_brand(){
  $dataArray = CSVReader::readcsv('modules/iCarrent/data/car_brand.csv');
  if ($dataArray){
    DBUtil::insertObjectArray($dataArray, 'icarrent_brand', true);
    unset($dataArray);
  }
}
function init_mobel(){
  $dataArray = CSVReader::readcsv('modules/iCarrent/data/car_model.csv');
//var_dump($dataArray); exit;
  if ($dataArray){
    DBUtil::insertObjectArray($dataArray, 'icarrent_model', true);
    unset($dataArray);
  }

}
function init_property(){
  $dataArray = CSVReader::readcsv('modules/iCarrent/data/car_property.csv');
  if ($dataArray){
    DBUtil::insertObjectArray($dataArray, 'icarrent_carproperty', true);
    unset($dataArray);
  }
}
function init_subcategory_brand(){
  $dataArray = CSVReader::readcsv('modules/iCarrent/data/car_subcategory_brand.csv');
  if ($dataArray){
    DBUtil::insertObjectArray($dataArray, 'icarrent_subcategory_brand', true);
    unset($dataArray);
  }
}
function init_subcategory_carproperty(){
  $dataArray = CSVReader::readcsv('modules/iCarrent/data/car_subcategory_carproperty.csv');
  if ($dataArray){
    DBUtil::insertObjectArray($dataArray, 'icarrent_subcategory_carproperty', true);
    unset($dataArray);
  }
}
function init_subcategory_model(){
  $dataArray = CSVReader::readcsv('modules/iCarrent/data/car_subcategory_model.csv');
  if ($dataArray){
    DBUtil::insertObjectArray($dataArray, 'icarrent_subcategory_model', true);
    unset($dataArray);
  }
}

function init_province(){
  $dataArray = CSVReader::readcsv('modules/iCarrent/data/province.csv');
  if ($dataArray){
    DBUtil::insertObjectArray($dataArray, 'icarrent_province', true);
    unset($dataArray);
  }
}
function init_district(){
  $dataArray = CSVReader::readcsv('modules/iCarrent/data/district.csv');
  if ($dataArray){
    DBUtil::insertObjectArray($dataArray, 'icarrent_district', true);
    unset($dataArray);
  }
}

function init_manual_category(){
    $dataArray= array("0"=>array("id"=>"1",
                          "formtype"=>"CAR",
                          "name"=>"รถบัส",
                          "prefix"=>"BS"
                          ),
                      "1"=>array("id"=>"2",
                                  "formtype"=>"CAR",
                                  "name"=>"รถบรรทุก",
                                  "prefix"=>"BTK"
                                  ),
                      "2"=>array("id"=>"3",
                                  "formtype"=>"CAR",
                                  "name"=>"รถตู้",
                                  "prefix"=>"BVN"
                                  ),
                      "3"=>array("id"=>"4",
                                  "formtype"=>"CAR",
                                  "name"=>"รถปิกอัพ",
                                  "prefix"=>"BPK"
                                  ),
                      "4"=>array("id"=>"5",
                                  "formtype"=>"CAR",
                                  "name"=>"เครื่องจักรกลหนัก",
                                  "prefix"=>"BMN"
                                  ),
                      "5"=>array("id"=>"6",
                                  "formtype"=>"OTHER",
                                  "name"=>"อะไหล่รถยนต์",
                                  "prefix"=>"BSP"
                                  ),
                      "6"=>array("id"=>"7",
                                  "formtype"=>"OTHER",
                                  "name"=>"ชุดแต่งรถ",
                                  "prefix"=>"BAC"
                                  ),
                      "7"=>array("id"=>"8",
                                  "formtype"=>"OTHER",
                                  "name"=>"บริการต่างๆ",
                                  "prefix"=>"BSV"
                                  ),
                      "8"=>array("id"=>"9",
                                  "formtype"=>"OTHER",
                                  "name"=>"งาน",
                                  "prefix"=>"BJB"
                                  )
                    );
    if ($dataArray){
      DBUtil::insertObjectArray($dataArray, 'icarrent_category', true);
      unset($dataArray);
    }
}


function init_level(){
  $dataArray = CSVReader::readcsv('modules/iCarrent/data/level.csv');
  if ($dataArray){
    DBUtil::insertObjectArray($dataArray, 'icarrent_level', true);
    unset($dataArray);
  }
}

function createFilesDirectory(){
    //Make [resource] directory
  if (!is_dir(UPLOAD_FILES_MAINPATH)) {
    mkdir(UPLOAD_FILES_MAINPATH, 0777);
  }
  //Make [icarrent_upload] directory
  if (!is_dir(UPLOAD_FILES_SUBPATH)) {
    mkdir(UPLOAD_FILES_SUBPATH, 0777);
  }
  //Make image  directory
  if (!is_dir(UPLOAD_IMAGE_ORIGINAL)) {
    mkdir(UPLOAD_IMAGE_ORIGINAL, 0777);
  }
  //Make thumb directory
  if (!is_dir(UPLOAD_IMAGE_THUMB)) {
    mkdir(UPLOAD_IMAGE_THUMB, 0777);
  }
  //Make file directory
  if (!is_dir(UPLOAD_FILES_SUBPATH_FILES)) {
    mkdir(UPLOAD_FILES_SUBPATH_FILES, 0777);
  }


  //copy .htaccess and index.html into resource directory
  copy(HTACCESS_PATH,UPLOAD_FILES_MAINPATH_HTACCESS_PATH);
  copy(HTML_BLANK_PATH,UPLOAD_FILES_MAINPATH_HTML_BLANK_PATH);

  //copy .htaccess and index.html into icarrent_upload directory
  copy(HTACCESS_PATH,UPLOAD_FILES_SUBPATH_HTACCESS_PATH);
  copy(HTML_BLANK_PATH,UPLOAD_FILES_SUBPATH_HTML_BLANK_PATH);

  //copy .htaccess and index.html into large directory
  copy(HTACCESS_PATH,UPLOAD_IMAGE_ORIGINAL_HTACCESS_PATH);
  copy(HTML_BLANK_PATH,UPLOAD_IMAGE_ORIGINAL_HTML_BLANK_PATH);

  //copy .htaccess and index.html into medium directory
  copy(HTACCESS_PATH,UPLOAD_IMAGE_THUMB_HTACCESS_PATH);
  copy(HTML_BLANK_PATH,UPLOAD_IMAGE_THUMB_HTML_BLANK_PATH);

  //copy .htaccess and index.html into medium directory
  copy(HTACCESS_PATH,UPLOAD_FILES_SUBPATH_FILES_HTACCESS_PATH);
  copy(HTML_BLANK_PATH,UPLOAD_FILES_SUBPATH_FILES_HTML_BLANK_PATH);

}

function rmdir_recurse($path)
{
    $path= rtrim($path, '/').'/';
    $handle = opendir($path);
    for (;false !== ($file = readdir($handle));)
        if($file != "." and $file != ".." )
        {
            $fullpath= $path.$file;
            if( is_dir($fullpath) )
            {
                rmdir_recurse($fullpath);
                rmdir($fullpath);
            }
            else
              unlink($fullpath);
        }
    closedir($handle);
}
